﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using YiSha.Util;
using YiSha.Util.Model;
using YiSha.Entity;
using YiSha.Model;
using YiSha.Admin.Web.Controllers;
using YiSha.Entity.QixingManage;
using YiSha.Business.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Web.Code;
using YiSha.Enum.QixingManage;
using YiSha.Util.Extension;

namespace YiSha.Admin.Web.Areas.QixingManage.Controllers
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-04-09 19:28
    /// 描 述：优惠劵控制器类
    /// </summary>
    [Area("QixingManage")]
    public class CouponController :  BaseController
    {
        private CouponBLL couponBLL = new CouponBLL();

        #region 视图功能
        [AuthorizeFilter("qixing:coupon:view")]
        public ActionResult CouponIndex()
        {
            return View();
        }

        public ActionResult CouponForm()
        {
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("qixing:coupon:search")]
        public async Task<ActionResult> GetListJson(CouponListParam param)
        {
            OperatorInfo operatorInfo = await Operator.Instance.Current();
            if (operatorInfo.IsSystem == 0)
            {
                param.tenant_id = operatorInfo.tenant_id;
            }
            TData<List<CouponEntity>> obj = await couponBLL.GetList(param);
            return Json(obj);
        }

        /// <summary>
        /// 读取优惠劵字典并控制权限显示过滤
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetCouponTypeListJson()
        {
            OperatorInfo operatorInfo = await Operator.Instance.Current();
            TData<List<KeyValuePair<int, string>>> data = new TData<List<KeyValuePair<int, string>>>();

            var list = typeof(coupon_type__enum).EnumToDictionaryList();

            if (operatorInfo.IsSystem == 0)
            {
                list = list.Where(t => t.Key == 2 || t.Key == 4).ToList();
            }

            data.Data = list;

            return Json(data);
        }


        [HttpGet]
        [AuthorizeFilter("qixing:coupon:search")]
        public async Task<ActionResult> GetPageListJson(CouponListParam param, Pagination pagination)
        {
            OperatorInfo operatorInfo = await Operator.Instance.Current();
            if (operatorInfo.IsSystem == 0)
            {
                param.tenant_id = operatorInfo.tenant_id;
            }
            TData<List<CouponEntity>> obj = await couponBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> GetFormJson(long id)
        {
            TData<CouponEntity> obj = await couponBLL.GetEntity(id);
            return Json(obj);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [AuthorizeFilter("qixing:coupon:add,qixing:coupon:edit")]
        public async Task<ActionResult> SaveFormJson(CouponEntity entity)
        {
            OperatorInfo operatorInfo = await Operator.Instance.Current();
            if (operatorInfo.IsSystem == 0)
            {
                entity.tenant_id = operatorInfo.tenant_id;
            }
            TData<string> obj = await couponBLL.SaveForm(entity);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("qixing:coupon:delete")]
        public async Task<ActionResult> DeleteFormJson(string ids)
        {
            TData obj = await couponBLL.DeleteForm(ids);
            return Json(obj);
        }
        #endregion
    }
}
